function [celltab,xsim]=plotiw(d,v,min,max,step,save,desc,opath); 
%function [celltab,xsim]=plotiw(d,v,min,max,step,save,desc,opath); 
% If sigs~IG(v,d) then 1/sigs~Gamma(d/2,2/v)
% Plots of pdf 
% Legend provides quantiles 
% see also ingampr.m 
% min    minimum value to plot
% max    maximum value to plot
% step   step between points
% save   =1 save 
% fpath  file path 
% opath  output path
% Output
% -------
%  celltab   cell, contains percentiles 
%            First two rows contain mean and SD respectively
%  xsim      vector of simulated values used to compute percentiles 
%
% Modified 6/02/03

if nargin < 3; 
    min=0.001; 
    step=0.01; 
    max=2; 
    save=0; 
end

resetgen; 
numsim=100000; 
x=min:step:max; 
tot=length(x);
pds=zeros(tot,1);
ii=1; 
while ii <=tot; 
    pds(ii)=invwishlub(x(ii),d,v); 
    ii=ii+1; 
end; 
xsim=1./gamrnd(d/2,2/v,numsim,1); 
meansim=v/(d-2); 
sdsim=2*(meansim^2)/(d-4); 
sdsim=sqrt(sdsim); 
mstr=num2str(meansim); 
sstr=num2str(sdsim); 
dstr=num2str(d); 
vstr=num2str(v); 
t1=['IW (' dstr ',' vstr ')   mean : ' mstr ' sd: ' sstr ]; 
plot(x,pds); 
title(t1); 
xsim=sort(xsim); 
per=[0.001 0.01 0.05 0.1 0.5 0.9 0.95 0.99 0.999]; 
obs=floor(numsim*per);
obs=xsim(obs); 
obsc=num2cell(obs); 
perc=num2cell(per');
jj=1; tstr=length(perc); 
celltit=cell(tstr,1);
celltab=cell(tstr,2); 
for jj=1:tstr; 
    celltit{jj}=[perc{jj} '    ' obsc{jj}];
    celltab{jj,1}=perc{jj}; 
    celltab{jj,2}=obsc{jj};
end 
%plot(2000*per,obs,'*r','MarkerSize',10,'MarkerFaceColor','b'); ; 
legend('density',celltit{1},celltit{2},celltit{3},celltit{4},celltit{5},celltit{6},celltit{7},celltit{8},celltit{9})
celladd=[{'1'} {mstr}; {'2'} {sstr}]; 
celltab=[celladd;celltab]; 
if save==1 
    desc=['IW' dstr '_' vstr]; 
    savegnew(opath,desc)
end